package com.rihel.servlets.User;

import com.rihel.dao.UserDAO;
import com.rihel.model.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class RegisterServlet extends HttpServlet {
    private UserDAO userDAO = new UserDAO();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        try {
            String uname = request.getParameter("uname");
            String pwd = request.getParameter("pwd");
            String mobile = request.getParameter("mobile");
            User user = null;
            if (!userDAO.isRegister(uname)) {
                user = new User();
                user.setUname(uname);
                user.setPwd(pwd);
                user.setMobile(mobile);
                userDAO.addUser(user);
                response.sendRedirect("result.jsp?code=2");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
